﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface IUsersDAL : IDAL<Users>
    {
        int Login(string name, string password, int iType, ref Users user);
        List<vUserPermission> GetUserPermission(Guid UserId);
    }

    public class UsersDAL : BaseDAL<Users>, IUsersDAL
    {
        public UsersDAL(IUnitOfWork context)
            : base(context)
        {
        }

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <param name="iType">1：键盘输入，2：扫描输入</param>
        /// <param name="user"></param>
        /// <returns></returns>
        public int Login(string name, string password, int iType, ref Users user)
        {
            user = dataContext.Users.FirstOrDefault(c => c.Name == name || c.Code == name || c.ICCard == name);
            if (user == null)
                return -1;
            if (iType == 1)
            {
                if (password.Equals(user.Password))
                    return 0;
                else
                    return -2;
            }
            else
                return 0;
        }

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<vUserPermission> GetUserPermission(Guid UserId)
        {
            return dataContext.vUserPermission.Where(t => t.UserId == UserId).ToList();
        }
    }
}
